package tests;

import java.lang.reflect.Method;

/**
 * @author:雷淑娥
 * @Date:2024/12/5 14:48
 * version:1.0
 */
public class Test03 {
    public static void main(String[] args) {
        try {
            Class clz = Class.forName("reflect.pojo.Person");

            Method getAge = clz.getDeclaredMethod("getAge",null);
            getAge.setAccessible(true);
            Object returnAge = getAge.invoke(null,null);
            System.out.println("年龄是：" + returnAge);

            Object person = clz.newInstance();
            Method silentMethod = clz.getDeclaredMethod("silentMethod",null);
            silentMethod.setAccessible(true);
            silentMethod.invoke(person,null);

            Method setName = clz.getDeclaredMethod("setName", String.class);
            setName.invoke(person,"kary");

            Object returnName = clz.getDeclaredMethod("getName").invoke(person);
            System.out.println("刚才设定的name是：" + returnName);
        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
